/**
 * Wihidum - Efficient Broker Hierarchy for Publish Subscribe Middleware
 *
 * License      :   Apache License 2.0
 * Home Page    :   http://code.google.com/p/wihidum/
 * Mailing List :   wihidum@googlegroups.com
 */

package lk.mrt.cse.wihidum.publisher.client;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * The UI of publisher service client
 */
public class UserInterface
        extends JFrame
{
    private Client client = new Client (this);

    private JTextField adminAddress = new JTextField ("http://localhost:8080/axis2/services/Wihidum-Admin-Service");
    private JTextField brokerAddress = new JTextField ("http://localhost:8080/axis2/services/Wihidum-Broker-Service");
    private JTextField publisherAddress = new JTextField ("http://localhost:8080/axis2/services/Wihidum-Publisher-Service");
    private JTextField topic = new JTextField ();
    private JTextArea publicationText = new JTextArea ();
    private JButton publishCommand = new JButton ("Publish");
    private JButton cancelCommand = new JButton ("Cancel");
    private JLabel informationBar = new JLabel ("");

    /**
     * Default constructor
     */
    public UserInterface ()
    {
        initComponents ();
    }

    /**
     * Add components to the UI
     */
    public void initComponents ()
    {

        JPanel inputPanel = new JPanel (new GridBagLayout ());
        GridBagConstraints constraints = new GridBagConstraints ();
        inputPanel.setBorder (BorderFactory.createEmptyBorder (10, 30, 10, 30));

        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 1;
        constraints.fill = GridBagConstraints.NONE;
        constraints.weightx = 0;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        inputPanel.add (new JLabel ("Admin Address"), constraints);

        constraints.gridx = 1;
        constraints.gridy = 0;
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.weightx = 1;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        inputPanel.add (adminAddress, constraints);

        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.gridwidth = 1;
        constraints.fill = GridBagConstraints.NONE;
        constraints.weightx = 0;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        inputPanel.add (new JLabel ("Broker Address"), constraints);

        constraints.gridx = 1;
        constraints.gridy = 1;
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.weightx = 1;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        inputPanel.add (brokerAddress, constraints);

        constraints.gridx = 0;
        constraints.gridy = 2;
        constraints.gridwidth = 1;
        constraints.weightx = 0;
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        inputPanel.add (new JLabel ("Publisher Addresss"), constraints);

        constraints.gridx = 1;
        constraints.gridy = 2;
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.weightx = 1;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        inputPanel.add (publisherAddress, constraints);

        constraints.gridx = 0;
        constraints.gridy = 3;
        constraints.gridwidth = 1;
        constraints.weightx = 0;
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        inputPanel.add (new JLabel ("Topic"), constraints);

        constraints.gridx = 1;
        constraints.gridy = 3;
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.weightx = 1;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        inputPanel.add (topic, constraints);

        JScrollPane scrollPane = new JScrollPane ();
        scrollPane.setViewportView (publicationText);
        scrollPane.setHorizontalScrollBarPolicy (ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        publicationText.setLineWrap (true);
        publicationText.setWrapStyleWord (true);

        JPanel infoPanel = new JPanel (new BorderLayout (0, 10));
        infoPanel.setBorder (BorderFactory.createEmptyBorder (10, 30, 10, 30));
        infoPanel.add (scrollPane, BorderLayout.CENTER);
        infoPanel.add (informationBar, BorderLayout.SOUTH);

        JPanel commandPanel = new JPanel (new GridLayout (1, 3));
        commandPanel.setBorder (BorderFactory.createEmptyBorder (10, 150, 10, 150));
        commandPanel.add (publishCommand);
        commandPanel.add (cancelCommand);

        JPanel backPanel = new JPanel (new BorderLayout ());
        backPanel.add (inputPanel, BorderLayout.NORTH);
        backPanel.add (infoPanel, BorderLayout.CENTER);
        backPanel.add (commandPanel, BorderLayout.SOUTH);
        this.setContentPane (backPanel);

        this.setSize (600, 300);
        Dimension screenSize = Toolkit.getDefaultToolkit ().getScreenSize ();
        setBounds ((screenSize.width - 600) / 2, (screenSize.height - 300) / 2, 600, 300);

        this.setDefaultCloseOperation (EXIT_ON_CLOSE);
        this.setTitle ("Publisher Service Client");

        publishCommand.addActionListener (new ActionListener()
        {
            public void actionPerformed (ActionEvent event)
            {
                publishActionPerformed (event);
            }
        });

        cancelCommand.addActionListener (new ActionListener()
        {
            public void actionPerformed (ActionEvent event)
            {
                cancelActionPerformed (event);
            }
        });
    }

    public void showInfo (String info)
    {
        informationBar.setText (info);
        informationBar.setForeground (Color.BLUE);
        this.repaint ();
    }

    public void showError (String info)
    {
        informationBar.setText (info);
        informationBar.setForeground (Color.RED);
        this.repaint ();
    }

    private void publishActionPerformed (ActionEvent event)
    {
        String brokerEPR = brokerAddress.getText ();
        if (brokerEPR == null || brokerEPR.isEmpty ())
        {
            showError ("Please set the end point address of the broker service");
        }

        String publisherEPR = publisherAddress.getText ();
        if (publisherEPR == null || publisherEPR.isEmpty ())
        {
            showError ("Please set the end point address of the publisher service");
        }

        String adminEPR = adminAddress.getText();
        if (adminEPR == null || adminEPR.isEmpty ())
        {
            showError ("Please set the end point address of the publisher service");
        }

        String topicText = topic.getText ();
        if (topicText == null || topicText.isEmpty ())
        {
            showError ("Please enter the topic of the publication");
        }

        String publicationBody = publicationText.getText ();
        if (publicationBody == null || publicationBody.isEmpty ())
        {
            showError ("Please fill the publication text");
        }

        client.publish (brokerEPR, adminEPR, publisherEPR, topicText, publicationBody);
    }

    private void cancelActionPerformed (ActionEvent event)
    {
        System.exit (0);
    }
}
